/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.artic.jme3gui.client;
import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.RenderState;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial.CullHint;
import de.lessvoid.nifty.Nifty;

public class MenuState extends AbstractAppState {


    protected Node rootNode = new Node(ApplicationConstants.ROOT_NODE);
    protected Node guiNode = new Node(ApplicationConstants.GUI_NODE);

    protected BitmapText menuText;
    protected BitmapFont menuFont;

    private ApplicationClient game = null;
    private Nifty nifty;
    private NiftyJmeDisplay niftyDisplay;

    private AppActionListener actionListener = new AppActionListener();

    public MenuState(ApplicationClient game) {
       this.game = game;
    }

    private class AppActionListener implements ActionListener {
        @Override
        public void onAction(String name, boolean value, float tpf) {
            if (!value)
                return;
            // Load other state
            game.loadGame();
        }
    }
/*
    public void loadFPSText(){
       menuFont = game.getAssetManager().loadFont("Interface/Fonts/Default.fnt");
       menuText = new BitmapText(menuFont, false);
       menuText.setSize(menuFont.getCharSet().getRenderedSize());
       menuText.setLocalTranslation(0, (game.getContext().getSettings().getHeight()/2f)-(menuText.getLineHeight()/2f), 0);
       menuText.setText("Frames per second");
       guiNode.attachChild(menuText);
    }
*/
    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);

         niftyDisplay = new NiftyJmeDisplay(game.getAssetManager(),
                game.getInputManager(),
                game.getAudioRenderer(),
                game.getGUIViewPort());

      nifty = niftyDisplay.getNifty();

      nifty.fromXml("Interface/hellojme.xml", "start");

      // attach the nifty display to the gui view port as a processor
      game.getGUIViewPort().addProcessor(niftyDisplay);

        // enable depth test and back-face culling for performance
       app.getRenderer().applyRenderState(RenderState.DEFAULT);

        guiNode.setQueueBucket(Bucket.Gui);
        guiNode.setCullHint(CullHint.Never);
  //      loadFPSText();

        // Init input
        if (game.getInputManager() != null){
            game.getInputManager().addMapping(ApplicationConstants.MENU_EXIT, new KeyTrigger(KeyInput.KEY_ESCAPE));
        }
    }

    @Override
    public void update(float tpf) {
        super.update(tpf);
//        menuText.setText("Press [Escape] to go to the Game-State");
        // simple update and root node
        rootNode.updateLogicalState(tpf);
        guiNode.updateLogicalState(tpf);
        rootNode.updateGeometricState();
        guiNode.updateGeometricState();
    }


    @Override
    public void stateAttached(AppStateManager stateManager) {
        game.getInputManager().addListener(new AppActionListener(), ApplicationConstants.MENU_EXIT);

        game.getViewPort().attachScene(rootNode);
        game.getGUIViewPort().attachScene(guiNode);
        if (niftyDisplay != null) game.getGUIViewPort().addProcessor(niftyDisplay);
        game.getContext().getMouseInput().setCursorVisible(true);
    }

    @Override
    public void stateDetached(AppStateManager stateManager) {
        game.getInputManager().removeListener(actionListener);
        game.getViewPort().detachScene(rootNode);
        game.getGUIViewPort().detachScene(guiNode);
        if (niftyDisplay != null) game.getGUIViewPort().removeProcessor(niftyDisplay);
        game.getContext().getMouseInput().setCursorVisible(false);
    }

    @Override
    public void render(RenderManager rm) {
    }
}